package com.tyq.util;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * Created by tyq on 16/10/20.
 */
public class Utils {


    /**
     * 返回消息到页面
     * @param resp
     * @param message 消息
     * @throws IOException
     */
    public static void responseAjax(HttpServletResponse resp,String message) throws IOException{
        //将消息读入流中
        InputStream is = new ByteArrayInputStream(message.getBytes("utf-8"));
        //设置响应编码
        resp.setCharacterEncoding("utf-8");
        //获取响应输出流
        ServletOutputStream sos = resp.getOutputStream();
        byte[] b = new byte[1024*1024];
        int len = 0;
        while ((len = is.read(b)) > 0){
            //将消息写入页面
            sos.write(b,0,len);
        }
        sos.flush();
        sos.close();
        is.close();
    }
}
